package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.plants;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.log.SysLogBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.COrderDetail;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CWeightSheets;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.SysUser;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.TPlants;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICOrderDetailService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICWeightSheetsService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ITPlantsService;
import com.xiaoxu.intranetweb.util.SzResult;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class PlantsBu {
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ITPlantsService plantsService;
    @Autowired
    private SysLogBu sysLogBu;
    @Autowired
    private ICWeightSheetsService cWeightSheetsService;
    @Autowired
    private ICOrderDetailService cOrderDetailService;

    public SzResult getPlants(String value) {
        Map map = new HashMap();
        QueryWrapper<TPlants> queryWrapper = new QueryWrapper<>();
        if (value != null && !value.equals("")) {
            queryWrapper.and(wrapper -> wrapper.like( "PLANT_FULL_NAME", value).or().like( "PLANT_SHORT_NAME", value));
        }
        queryWrapper.eq("DEL_FLAG", 0);
        List<TPlants> list = plantsService.list(queryWrapper);
        map.put("total", list.size());
        map.put("list", list);
        return SzResult.success(map);
    }

    public SzResult addPlants(String plantFullName, String plantShortName, String token,String plantId) throws Exception {
        TPlants plants = new TPlants();
        if (plantFullName != null && !plantFullName.equals("")) {
            QueryWrapper<TPlants> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PLANT_FULL_NAME", plantFullName);
            queryWrapper.eq("DEL_FLAG", 0);
            TPlants plants1 = plantsService.getOne(queryWrapper);
            if (plants1 != null) {
                return SzResult.error("该工厂已存在");
            }
            plants.setPlantFullName(plantFullName);
        } else {
            return SzResult.error("工厂全称不能为空");
        }
        if (plantShortName != null && !plantShortName.equals("")) {
            QueryWrapper<TPlants> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("PLANT_SHORT_NAME", plantShortName);
            queryWrapper.eq("DEL_FLAG", 0);
            TPlants plants1 = plantsService.getOne(queryWrapper);
            if (plants1 != null) {
                return SzResult.error("该工厂简称已存在");
            }
            plants.setPlantShortName(plantShortName);
        } else {
            return SzResult.error("工厂简称不能为空");
        }
        plants.setDelFlag(0);
        plantsService.save(plants);
        SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
        //sysLogBu.logRecord("修改区域管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的区域管理");
        sysLogBu.logRecord("添加工厂管理", user.getTrueName(), "添加了工厂名称为" + plantFullName + "的工厂管理", plantId);
        return SzResult.Success();
    }

    public SzResult updatePlants(String plantFullName, String plantShortName, String plantId, String token) throws Exception {
        if (plantId != null && !plantId.equals("")) {
            TPlants plants = plantsService.getById(plantId);
            if (plants == null) {
                return SzResult.error("该工厂不存在");
            }
            if (plantFullName != null && !plantFullName.equals("")) {
                QueryWrapper<TPlants> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("PLANT_FULL_NAME", plantFullName);
                queryWrapper.eq("DEL_FLAG", 0);
                queryWrapper.ne("ID", plantId);
                TPlants plants1 = plantsService.getOne(queryWrapper);
                if (plants1 != null) {
                    return SzResult.error("该工厂已存在");
                }
                plants.setPlantFullName(plantFullName);
            }
            if (plantShortName != null && !plantShortName.equals("")) {
                plants.setPlantShortName(plantShortName);
            }
            plantsService.updateById(plants);
            SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
            //sysLogBu.logRecord("修改区域管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的区域管理");
            sysLogBu.logRecord("修改工厂管理", user.getTrueName(), "修改了工厂名称为" + plantFullName + "的工厂管理",plantId);
        } else {
            return SzResult.error("请选择工厂");
        }
        return SzResult.Success();
    }

    public SzResult deletePlants(String plantId, String token,String plantId1) throws Exception {
        QueryWrapper<CWeightSheets> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATA_FROM", plantId);
        queryWrapper.eq("WEISHT_DELETED", 0);
        List<CWeightSheets> list = cWeightSheetsService.list(queryWrapper);
        if (list.size() > 0) {
            return SzResult.error("该工厂已被磅单使用，无法删除");
        }
        QueryWrapper<COrderDetail> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("DATA_FROM", plantId);
        queryWrapper1.eq("ORDETAIL_DELETED", 0);
        List<COrderDetail> list1 = cOrderDetailService.list(queryWrapper1);
        if (list1.size() > 0) {
            return SzResult.error("该工厂存在余额账户，无法删除");
        }

        if (plantId != null && !plantId.equals("")) {
            TPlants tPlant = plantsService.getById(plantId);
            if (tPlant != null) {
                tPlant.setDelFlag(1);
                plantsService.updateById(tPlant);
                //日志
                SysUser user = userInfoDao.getUser(StpUtil.getLoginIdByToken(token).toString());
                //sysLogBu.logRecord("修改区域管理", user.getTrueName(), "修改了区域名称为" + ashGateVideoBind.getAshgateName() + "的区域管理");
                sysLogBu.logRecord("删除工厂", user.getTrueName(), "删除了工厂名称为" + tPlant.getPlantFullName() + "的工厂", plantId1);
                return SzResult.Success();
            } else {
                return SzResult.error("工厂不存在");
            }
        } else {
            return SzResult.error("请选择要删除的工厂");
        }
    }
}
